package com.qqtech.qquan.user.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.pub.model.PubUpgradeVo;
import com.qqtech.qquan.pub.service.PubUpgradeService;
import com.qqtech.qquan.user.model.UserSpoor;
import com.qqtech.qquan.user.model.UserSpoorQuery;
import com.qqtech.qquan.user.model.UserSpoorVo;
import com.qqtech.qquan.user.service.UserSpoorService;
import com.qqtech.qquan.user.service.UserValueService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/spoor")
public class UserSpoorController extends BaseWebController<UserSpoor, UserSpoorQuery> {
	@Resource
	private UserSpoorService userSpoorService;
	@Resource
	private UserValueService userValueService;
	@Resource
	private PubUpgradeService pubUpgradeService;

	@Override
	protected BaseService<UserSpoor> getBaseService() {
		return userSpoorService;
	}

	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody UserSpoorVo userSpoorVo) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		// UserValue userValue = null;
		UserSpoor userSpoor = (UserSpoor) userSpoorVo;
		if (curUserId != null) {
			userSpoor.setUserId(curUserId);
			// userValue = userValueService.getById(curUserId);
		} else {
			userSpoor.setUserId(0);
		}
		if (StringUtil.isNotBlank(userSpoorVo.getPlatformCodeStr())) {
			PubUpgradeVo pubUpgradeVo = pubUpgradeService
					.getUpgradeByPlatform(new Integer(userSpoorVo.getPlatformCodeStr()).intValue());
			if (pubUpgradeVo != null) {
				userSpoor.setContentServerVersion(pubUpgradeVo.getUpdVersion());
				userSpoor.setNativeServerVersion(pubUpgradeVo.getNativeVersion());
				userSpoor.setPubAt(TimeUtil.now());
			}
		}
		userSpoor.setIp(StringUtil.getClientIp(super.getRequest()));
		userSpoorService.insert(userSpoor);
		/*
		 * if (userValue == null) { if (curUserId != null) { userValue = new
		 * UserValue(); userValue.setId(curUserId); userValue.setMoney(0);
		 * userValue.setX(userSpoor.getX()); userValue.setY(userSpoor.getY());
		 * userValue.setIsBackground(YesNoEnum.NO.getCode());
		 * userValue.setIsShowLocation(YesNoEnum.NO.getCode());
		 * userValue.setOsType(UserValueOsTypeDictEnum.ANDROID.getCode());
		 * userValueService.insert(userValue); } } else { if (curUserId != null
		 * && userSpoor.getX() != null && userSpoor.getY() != null &&
		 * (userValue.getX() == null || userValue.getY() == null ||
		 * userSpoor.getX().compareTo(userValue.getX()) != 0 ||
		 * userSpoor.getY().compareTo(userValue.getY()) != 0)) { UserValue
		 * updateUserValue = new UserValue();
		 * updateUserValue.setId(userValue.getId());
		 * updateUserValue.setX(userSpoor.getX());
		 * updateUserValue.setY(userSpoor.getY());
		 * userValueService.updateById(updateUserValue); } }
		 */
		return ret;
	}
}
